package com.daxt.controller.filemanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesDocFileMapper;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.archives.ArchivesTestingStandardMapper;
import com.daxt.mapper.archives.ArchivesTypeDocMapper;
import com.daxt.model.service.archives.result.ArchivesList;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.archives.vo.ArchivesDocFile;
import com.daxt.model.service.archives.vo.ArchivesTestingStandard;
import com.daxt.model.service.archives.vo.ArchivesTypeDoc;
import com.daxt.service.TestingService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "规范性检查")
@RestController
@RequestMapping("/fileManager")
public class TestingController {
	@Autowired
	private ArchivesTestingStandardMapper archivesTestingStandardMapper;
	@Autowired
	private ArchivesDocMapper archivesDocMapper;
	@Autowired
	private ArchivesTypeDocMapper archivesTypeDocMapper;
	@Autowired
	private ArchivesDocFileMapper archivesDocFileMapper;
	@Autowired
	private TestingService testingService;
	@Autowired
	private RedisUtil redisUtil;
	@ApiOperation(value = "开始检查  state 0 检查中  1检查完成  2检查异常", notes = "")
	@PostMapping("/testing/check")
	public Result<String> check(@RequestBody List<String> ids) {
		String taskId = IdWorker.getIdStr();
		//QueryWrapper<ArchivesTestingStandard> queryWrapper=new QueryWrapper<ArchivesTestingStandard>();
		//queryWrapper.eq("typeId", typeId);
		//if(!StringUtils.isEmpty(startYear)&&!StringUtils.isEmpty(endYear)) {
			
		//}
		//List<ArchivesTestingStandard> list = archivesTestingStandardMapper.selectList(queryWrapper);
		List<ArchivesTestingStandard> list = archivesTestingStandardMapper.selectBatchIds(ids);
		if(list!=null&&list.size()>0) {
			Map<String,Object> result=new HashMap<String, Object>();
			testingService.StandardTesting(list.get(0).getTypeId(),list, taskId);
			result.put("state", "0");
			redisUtil.hset("testing", taskId,result,60*60);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", taskId);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"请先创建检查项目！！！", "");
		}
		
	}
	
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "规范性检测结果", notes = "")
	@GetMapping("/testing/check/result/{taskId}")
	public Result<Map<Long,Long>> result(@PathVariable("taskId")String taskId) {
		Object hget = redisUtil.hget("testing", taskId);
		if(hget!=null) {
			Map<Long,Long> map=(Map<Long,Long>)hget;
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", map);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"任务不存在或已过期！！！", null);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "规范性检测结果列表", notes = "")
	@GetMapping("/testing/check/result/{taskId}/{testId}")
	public Result<ArchivesList> result(@PathVariable("taskId")String taskId,@PathVariable("testId")String testId) {
		Object object = redisUtil.get(taskId+testId);
		List<ArchivesDoc> selectList=new ArrayList<ArchivesDoc>();
		
		ArchivesTestingStandard testing = archivesTestingStandardMapper.selectById(testId);
		if(testing==null) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "测试项不存在！！！", null);
		}
		QueryWrapper<ArchivesTypeDoc> queryWrapperTypeDoc=new QueryWrapper<ArchivesTypeDoc>();
		queryWrapperTypeDoc.eq("isList", "1");
		queryWrapperTypeDoc.eq("typeId", testing.getTypeId());
		queryWrapperTypeDoc.eq("extType", testing.getFileType());
		List<ArchivesTypeDoc> archivesTypeDocs = archivesTypeDocMapper.selectList(queryWrapperTypeDoc);
		if(archivesTypeDocs==null||archivesTypeDocs.size()==0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "测试项对应的门类不存在！！！", null);
		}
		if(object!=null) {
			List<ArchivesDoc> docIds=(List<ArchivesDoc>)object;
			QueryWrapper<ArchivesDoc> queryWrapper=new QueryWrapper<ArchivesDoc>();
			queryWrapper.in("id", docIds);
			queryWrapper.eq("state", "0");
			queryWrapper.eq("isDelete", "0");
			selectList = archivesDocMapper.selectList(queryWrapper);
			if(selectList!=null&&selectList.size()>0) {
				List<Long> docIds1 = new ArrayList<Long>();
					for (ArchivesDoc docE : selectList) {
						docIds1.add(docE.getId());
					}
					QueryWrapper<ArchivesDocFile> queryWrapperFile = new QueryWrapper<ArchivesDocFile>();
					queryWrapperFile.in("docId", docIds1);
					List<ArchivesDocFile> selectListTemp = archivesDocFileMapper.selectList(queryWrapperFile);
					for (int i = 0; i < selectList.size(); i++) {
						for (ArchivesDocFile archivesDocFile :selectListTemp) {
							if (archivesDocFile.getDocId() != null && archivesDocFile.getDocId().intValue() == selectList.get(i).getId().intValue()) {
								selectList.get(i).setIsFile("1");
							}
						}
					

				}
			}
		}
		ArchivesList result=new ArchivesList();
		result.setBaseCol(archivesTypeDocs);
		result.setCols(selectList);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", result);
	}
}
